package com.newbingo.util;

import java.io.Serializable;
import java.util.Set;

import com.newbingo.entity.Agent;


/*
 * 生成代理树形节点
 */
public class AgentTree implements Serializable {
	
//	public static StringBuffer getSubAgentTree(Agent agent){
//		StringBuffer sb=new StringBuffer();
//		sb.append("[");
//		Set<Agent> childAgents=agent.getChildAgent();
//		if(childAgents!=null && childAgents.size()>0){
//			for(Agent childAgent:childAgents){
//				sb.append("{");
//				sb.append("uno:'"+childAgent.getUno()+"'");
//				sb.append(",text:'"+childAgent.getTrueName()+"'");
//				if(childAgent.getChildAgent().size()>0){				//表示有子节点
//					sb.append(",children:").append(getAgentTree(childAgent));
//				}else{										//表示没有子节点,即叶节点
//					sb.append(",leaf:true");
//				}
//				sb.append("},");
//			}
//		}
//		sb.deleteCharAt(sb.length()-1);					//删除最后一个逗号","
//		sb.append("]");
//		return sb;
//	}
//	
	//递归调用，根据代理的set集合获取子代理（股东）的json字符
	public static StringBuffer getSubAgentTreeString(Set<Agent> agents){
		StringBuffer sb=new StringBuffer();
		sb.append("[");
		if(agents.size()>0){
			for(Agent agent: agents){
				sb.append("{");
				sb.append("uno:'"+agent.getUno()+"'");
				sb.append(",text:'"+agent.getTrueName()+"'");
				if(agent.getChildAgent()!=null && agent.getChildAgent().size()>0){
					sb.append(",children:").append(getSubAgentTreeString(agent.getChildAgent()));
				}else{
					sb.append(",leaf:true");
				}
				sb.append("},");
			}
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append("]");
		return sb;
	}
	
	
	public static StringBuffer getAgentTree(Agent agent){
		StringBuffer sb=new StringBuffer();
		sb.append("[{");
		sb.append("uno:'"+agent.getUno()+"'");
		sb.append(",text:"+agent.getTrueName()+"'");
		sb.append(",children:");
		sb.append(getSubAgentTreeString(agent.getChildAgent()));
		sb.append("}]");
		System.out.println(sb.toString());
		return sb;
	}

}
